<template>
  <div class="login-container">
    <div class="logo">
      <img src="@/assets/panda-logo.png" alt="熊猫社区">
      <h1>欢迎回来</h1>
    </div>
    
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          id="username" 
          class="form-control" 
          placeholder="请输入用户名" 
          required 
          v-model="username"
        >
      </div>
      
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          class="form-control" 
          placeholder="请输入密码" 
          required 
          v-model="password"
        >
      </div>
      
      <div class="form-group">
        <label for="captcha">验证码</label>
        <div class="captcha-group">
          <input 
            type="text" 
            id="captcha" 
            class="form-control" 
            placeholder="请输入验证码" 
            required 
            v-model="captcha"
          >
          <div class="captcha-img" @click="refreshCaptcha">
            <span>{{ captchaText }}</span>
          </div>
        </div>
      </div>
      
      <button type="submit" class="btn" :disabled="loading">
        {{ loading ? '登录中...' : '登录' }}
      </button>
      
      <div class="switch-form">
        还没有账号？<a href="#" @click.prevent="goToRegister">立即注册</a>
      </div>
      
      <div class="divider">或</div>
      
      <div class="social-login">
        <div class="social-btn" @click="handleSocialLogin('wechat')">
          <img src="@/assets/wechat-icon.png" alt="微信登录" width="24">
        </div>
        <div class="social-btn" @click="handleSocialLogin('qq')">
          <img src="@/assets/qq-icon.png" alt="QQ登录" width="24">
        </div>
        <div class="social-btn" @click="handleSocialLogin('weibo')">
          <img src="@/assets/weibo-icon.png" alt="微博登录" width="24">
        </div>
      </div>
    </form>
  </div>
</template>

<style scoped>
:root {
  --primary-color: #333333;
  --secondary-color: #4CAF50;
  --accent-color: #FF9800;
  --light-color: #f8f8f8;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Arial', sans-serif;
}

body {
  background-color: #f5f5f5;
  background-image: url('@/assets/panda-bg.png');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.login-container {
  width: 100%;
  max-width: 400px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  padding: 30px;
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo img {
  width: 80px;
  height: 80px;
}

.logo h1 {
  color: var(--primary-color);
  margin-top: 10px;
  font-size: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: var(--primary-color);
  font-weight: bold;
}

.form-control {
  width: 100%;
  padding: 12px 15px;
  border: 2px solid #ddd;
  border-radius: 10px;
  font-size: 16px;
  transition: all 0.3s;
}

.form-control:focus {
  border-color: var(--secondary-color);
  outline: none;
}

.captcha-group {
  display: flex;
  gap: 10px;
}

.captcha-img {
  background-color: #eee;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0 10px;
}

.btn {
  display: inline-block;
  padding: 12px 20px;
  background-color: var(--secondary-color);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s;
  width: 100%;
}

.btn:hover {
  background-color: #3e8e41;
  transform: translateY(-2px);
}

.switch-form {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.switch-form a {
  color: var(--secondary-color);
  text-decoration: none;
  font-weight: bold;
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
  color: #999;
}

.divider::before, .divider::after {
  content: "";
  flex: 1;
  border-bottom: 1px solid #ddd;
}

.divider::before {
  margin-right: 10px;
}

.divider::after {
  margin-left: 10px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
  border: 1px solid #ddd;
  cursor: pointer;
  transition: all 0.3s;
}

.social-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
</style>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import apiService from '@/services/api.service'

const router = useRouter()
const username = ref('')
const password = ref('')
const captcha = ref('')
const captchaText = ref('')
const loading = ref(false)

const generateCaptcha = () => {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  let result = ''
  for (let i = 0; i < 4; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return result
}

const refreshCaptcha = () => {
  captchaText.value = generateCaptcha()
  captcha.value = ''
}

const handleLogin = async () => {
  if (!username.value || !password.value) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  if (!captcha.value) {
    ElMessage.warning('请输入验证码')
    return
  }

  if (captcha.value.toLowerCase() !== captchaText.value.toLowerCase()) {
    ElMessage.error('验证码错误')
    refreshCaptcha()
    return
  }

  loading.value = true
  try {
    const response = await apiService.login(username.value, password.value)
    if (response.success) {
      // 保存用户信息和token
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('userId', response.data.userId)
      localStorage.setItem('username', response.data.username)
      
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error(response.message || '登录失败')
      refreshCaptcha()
    }
  } catch (error) {
    ElMessage.error('登录失败，请稍后重试')
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}

const handleSocialLogin = (platform) => {
  ElMessage.info(`${platform}登录功能开发中...`)
}

onMounted(() => {
  refreshCaptcha()
})
</script> 